﻿using System;
using System.Collections.Generic;
using System.Data;
using UDS.Components;
using System.Diagnostics;

namespace UDS.Entity
{
    public class Project
    {
        public int ID
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public string Description
        {
            get;
            set;
        }

        public int Type
        {
            get;
            set;
        }

        public int ParentID
        {
            get;
            set;
        }

        public string CreatedBy
        {
            get;
            set;
        }

        public DateTime TimeCreated
        {
            get;
            set;
        }

        public int Status
        {
            get;
            set;
        }

        public int Scale
        {
            get;
            set;
        }

        public DateTime StartDate
        {
            get;
            set;
        }

        public DateTime EndDate
        {
            get;
            set;
        }

        /// <summary>
        /// Get the number of documents approved in this project.
        /// </summary>
        public int FileCount
        {
            get;
            set;
        }

        /// <summary>
        /// 获得用户所有的项目
        /// </summary>
        public static IList<Project> GetProjects(string userName)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@UserName", SqlDbType.NVarChar, 300, userName)
            };

            var list = new List<Project>();
            using (var dr = db.RunDataReader("dbo.sproc_GetTaskClass_V2", parameters))
            {
                while (dr.Read())
                {
                    list.Add(ReadProject(dr));
                }
            }
            return list;
        }

        private static Project ReadProject(IDataReader dr)
        {
            Project p = new Project();
            p.ID = (int)dr["ClassID"];
            p.Name = Convert.ToString(dr["ClassName"]);
            p.Description = Convert.ToString(dr["ClassRemark"]);
            p.Type = Utility.ReadInt32(dr["ClassType"], 0);
            p.ParentID = Utility.ReadInt32(dr["ClassParentID"], 0);
            p.TimeCreated = Utility.ReadSQLDate(dr["ClassAddedDate"]);
            p.CreatedBy = Convert.ToString(dr["ClassAddedBy"]);
            p.Status = Utility.ReadInt32(dr["Status"], 0);
            p.Scale = Utility.ReadInt32(dr["Scale"], 0);
            p.StartDate = Utility.ReadSQLDate(dr["StartDate"]);
            p.StartDate = Utility.ReadSQLDate(dr["EndDate"]);
            p.FileCount = Utility.ReadInt32(dr["FileCount"], 0);

            return p;
        }
    }
}
